package com.ghtmlx.client.grid;

import java.util.HashMap;
import java.util.Map;

import com.ghtmlx.client.menu.MenuObject;
import com.ghtmlx.client.util.DataType;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;

/**
 * User: j.voronin
 * Date: 20.01.2010
 */
public class Grid extends Widget {

    public static final String SKYBLUE_SKIN = "dhx_skyblue";

    public static final String IMAGE_PATH_DEFAULT = "js/dhtmlxSuite/dhtmlxGrid/codebase/imgs/";
    // ###############################
    // Column type definition
    // ###############################

    /**
     * There are some predefined cell editors delivered with dhtmlxGrid. They are:<br> <li>ReadOnly (<strong>ro</strong>) -
     * cell can't be edited</li> <li>Simple Editor (<strong>ed</strong>) - text is edited inside cell</li> <li>Text
     * Editor (<strong>txt</strong>) - text is edited in popup multiline textarea</li> <li>Checkbox (<strong>ch</strong>) -
     * standard checkbox</li> <p/> <li>Radiobutton (<strong>ra</strong>) - column oriented radiobutton</li> <li>Select
     * box (<strong>coro</strong>) - simple selectbox</li> <li>Combobox (<strong>co</strong>) - select box with
     * ability to add some other value</li> <li>Image (<strong>img</strong>) - not editable. Value considered as url
     * of image</li> <p/> <br><strong>Special types:</strong><br> <p/> <li>Color picker (<strong>cp</strong>) -
     * simple color picker (just for example). Value considered as color code or name</li> <li>Price oriented (<strong>price</strong>) -
     * shows $ before value, all values eql 0 shown as na in red color</li> <li>Dynamic of Sales (<strong>dyn</strong>) -
     * shows up/down icon depending on value. Also color coding available (green/red)</li>
     */
    public final static String ED = "ed";

    public final static String RA = "ra";

    public final static String RO = "ro";

    public final static String CO = "co";

    public final static String CH = "ch";

    public final static String DYN = "dyn";

    public final static String TXT = "txt";

    public final static String TREE = "tree";
    // ###############################
    // Allowed value's for column align
    // ###############################

    public final static String LEFT = "left";

    public final static String CENTER = "center";

    public final static String RIGHT = "right";
    // ###############################
    // Allowed value's to define sorting options
    // ###############################

    public final static String STR = "str";

    public final static String INT = "int";

    public final static String SERVER = "server";

    public final static String CUSTOMSORTING = "customsorting";


    private GridObject gridObject = null;

    private Map<Integer, Object> values = new HashMap<Integer, Object>();

    public Grid() {
        setElement(DOM.createDiv());
        gridObject = create(getElement());
        attachEvents();
    }

    public Grid(Element parentElement) {
        gridObject = create(parentElement);
        attachEvents();
    }

    protected GridObject create(Element container) {
        return GridObject.create(container);
    }

    public void attachEvents() {
        gridObject.attachEvents();
    }

    public void addRow(int row_ind, Object[] data) {
        gridObject.addRow(row_ind, data);
    }

    public void addRow(int newInd, Object[] data, Object value) {
        gridObject.addRow(newInd, data);
        values.put(newInd, value);
    }

    public void addRowOnPosition(int row_ind, Object[] dataArray, int index) {
        gridObject.addRowOnPosition(row_ind, dataArray, index);
    }

    public void adjustColumnSize(int col_ind) {
        gridObject.adjustColumnSize(col_ind);
    }

    public void attachHeader(Object[] dataArray, String[] styleArray) {
        gridObject.attachHeader(dataArray, styleArray);
    }

    public void attachToObject(JavaScriptObject obj) {
        gridObject.attachToObject(obj);
    }

    public CellObject cellById(int row_ind, int col) {
        return gridObject.cellById(row_ind, col);
    }

    public CellObject cellByIndex(int row_ind, int col) {
        return gridObject.cellByIndex(row_ind, col);
    }

    public void changeRowId(int old_rowInd, int new_rowInd) {
        gridObject.changeRowId(old_rowInd, new_rowInd);
    }

    public void checkAll(boolean mode) {
        gridObject.checkAll(mode);
    }

    public void clearAll(boolean header) {
        gridObject.clearAll(header);
    }

    public void clearAndLoad(String url, String type) {
        gridObject.clearAndLoad(url, type);
    }

    public void clearSelection() {
        gridObject.clearSelection();
    }

    public void collectValues(int column) {
        gridObject.collectValues(column);
    }

    public void copyBlockToClipboard() {
        gridObject.copyBlockToClipboard();
    }

    public void copyRowContent(int from_row_ind, int to_row_ind) {
        gridObject.copyRowContent(from_row_ind, to_row_ind);
    }

    public void deleteRow(int row_ind) {
        gridObject.deleteRow(row_ind);
    }

    public void deleteSelectedRows() {
        gridObject.deleteSelectedRows();
    }

    public void destructor() {
        gridObject.destructor();
    }

    public void detachEvent(String eventId) {
        gridObject.detachEvent(eventId);
    }

    public void detachHeader(int index) {
        gridObject.detachHeader(index);
    }

    public void doesRowExist(int row_ind) {
        gridObject.doesRowExist(row_ind);
    }

    public void editCell() {
        gridObject.editCell();
    }

    public void editStop(boolean mode) {
        gridObject.editStop(mode);
    }

    public void enableAlterCss(String even, String odd) {
        gridObject.enableAlterCss(even, odd);
    }

    public void enableAutoHeight(boolean mode) {
        gridObject.enableAutoHeight(mode);
    }

    public void enableAutoHeight(boolean mode, int maxHeight) {
        gridObject.enableAutoHeight(mode, maxHeight);
    }

    public void enableAutoHeight(boolean mode, int maxHeight, boolean countFullHeight) {
        gridObject.enableAutoHeight(mode, maxHeight, countFullHeight);
    }

    public void enableAutoWidth(boolean mode) {
        gridObject.enableAutoWidth(mode);
    }

    public void enableAutoWidth(boolean mode, int max_limit, int min_limit) {
        gridObject.enableAutoWidth(mode, max_limit, min_limit);
    }

    public void enableBlockSelection(boolean mode) {
        gridObject.enableBlockSelection(mode);
    }

    public void enableCellIds(boolean mode) {
        gridObject.enableCellIds(mode);
    }

    public void enableContextMenu(MenuObject menu) {
        gridObject.enableContextMenu(menu);
    }

    public void enableDragAndDrop(boolean mode) {
        gridObject.enableDragAndDrop(mode);
    }

    public void enableDragOrder(boolean mode) {
        gridObject.enableDragOrder(mode);
    }

    public void enableEditEvents(boolean click, boolean dblclick, boolean f2key) {
        gridObject.enableEditEvents(click, dblclick, f2key);
    }

    public void enableEditTabOnly(boolean mode) {
        gridObject.enableEditTabOnly(mode);
    }

    public void enableHeaderImages(boolean mode) {
        gridObject.enableHeaderImages(mode);
    }

    public void enableKeyboardSupport(boolean mode) {
        gridObject.enableKeyboardSupport(mode);
    }

    public void enableLightMouseNavigation(boolean mode) {
        gridObject.enableLightMouseNavigation(mode);
    }

    public void enableMarkedCells(boolean mode) {
        gridObject.enableMarkedCells(mode);
    }

    public void enableMultiline(boolean state) {
        gridObject.enableMultiline(state);
    }

    public void enableMultiselect(boolean state) {
        gridObject.enableMultiselect(state);
    }

    public void enableResizing(Boolean[] allowds) {
        gridObject.enableResizing(allowds);
    }

    public void enableRowsHover(boolean state, String cssCls) {
        gridObject.enableRowsHover(state, cssCls);
    }

    public void enableSmartRendering(boolean mode, int buffer) {
        gridObject.enableSmartRendering(mode, buffer);
    }

    public void enableStableSorting(boolean mode) {
        gridObject.enableStableSorting(mode);
    }

    public void enableTooltips(Boolean[] allowds) {
        gridObject.enableTooltips(allowds);
    }

    public void forceLabelSelection(boolean mode) {
        gridObject.forceLabelSelection(mode);
    }

    public JavaScriptObject getAllRowIds() {
        return gridObject.getAllRowIds();
    }

    public JavaScriptObject getCheckedRows(int col_ind) {
        return gridObject.getCheckedRows(col_ind);
    }

    public String getColIndexById(String id) {
        return gridObject.getColIndexById(id);
    }

    public String getColType(int col_ind) {
        return gridObject.getColType(col_ind);
    }

    public String getColTypeById(String id) {
        return gridObject.getColTypeById(id);
    }

    public String getColWidth(int col_ind) {
        return gridObject.getColWidth(col_ind);
    }

    public int getColWidthAsInteger(int col_ind) {
        return gridObject.getColWidthAsInteger(col_ind);
    }

    public String getColumnId(int col_ind) {
        return gridObject.getColumnId(col_ind);
    }

    public String getColumnLabel(int col_ind) {
        return gridObject.getColumnLabel(col_ind);
    }

    public int getColumnsNum() {
        return gridObject.getColumnsNum();
    }

    public JavaScriptObject getCombo(int col_ind) {
        return gridObject.getCombo(col_ind);
    }

    public JavaScriptObject getCustomCombo(String row_id, int col_ind) {
        return gridObject.getCustomCombo(row_id, col_ind);
    }

    public JavaScriptObject getFilterElement(int col_ind) {
        return gridObject.getFilterElement(col_ind);
    }

    public String getFooterLabel(int col_ind, int frow_ind) {
        return gridObject.getFooterLabel(col_ind, frow_ind);
    }

    public JavaScriptObject getMarked() {
        return gridObject.getMarked();
    }

    public String getRowAttribute(String rowId, String name) {
        return gridObject.getRowAttribute(rowId, name);
    }

    public String getRowId(int row_ind) {
        return gridObject.getRowId(row_ind);
    }

    public int getRowIndex(String row_id) {
        return gridObject.getRowIndex(row_id);
    }

    public int getRowsNum() {
        return gridObject.getRowsNum();
    }

    public int getSelectedCellIndex() {
        return gridObject.getSelectedCellIndex();
    }

    public String getSelectedRowId() {
        return gridObject.getSelectedRowId();
    }

    public JavaScriptObject getSortingState() {
        return gridObject.getSortingState();
    }

    public JavaScriptObject getStateOfView() {
        return gridObject.getStateOfView();
    }

    public void hidePivot() {
        gridObject.hidePivot();
    }

    public void init() {
        gridObject.init();
    }

    public void load(String url, DataType type) {
        gridObject.load(url, type);
    }

    public void lockRow(String row_id, boolean mode) {
        gridObject.lockRow(row_id, mode);
    }

    public void mark(int row_ind, int cell_ind, boolean state) {
        gridObject.mark(row_ind, cell_ind, state);
    }

    public void moveRowDown(String row_id) {
        gridObject.moveRowDown(row_id);
    }

    public void moveRowUp(String row_id) {
        gridObject.moveRowUp(row_id);
    }

    public void parse(String data, DataType type) {
        gridObject.parse(data, type);
    }

    public void pasteBlockFromClipboard() {
        gridObject.pasteBlockFromClipboard();
    }

    public void preventIECaching() {
        gridObject.preventIECaching();
    }

    public void rowToDragElement(Image image, String row_id, int cell_ind) {
        gridObject.rowToDragElement(image, row_id, cell_ind);
    }

    public void selectAll() {
        gridObject.selectAll();
    }

    public void selectCell(String row_ind, int cell_ind, boolean call_onSelect, boolean preserve, boolean edit, boolean show) {
        gridObject.selectCell(row_ind, cell_ind, call_onSelect, preserve, edit, show);
    }

    public void selectRow(int row_ind, boolean call_onSelect, boolean preserve, boolean show) {
        gridObject.selectRow(row_ind, call_onSelect, preserve, show);
    }

    public void selectRowById(String row_id, boolean call_onSelect, boolean preserve, boolean show) {
        gridObject.selectRowById(row_id, call_onSelect, preserve, show);
    }

    public void setAwaitedRowHeight(int height) {
        gridObject.setAwaitedRowHeight(height);
    }

    public void setCSVDelimiter(char delimiter) {
        gridObject.setCSVDelimiter(delimiter);
    }

    public void setCellTextStyle(String row_id, int cell_ind, String style) {
        gridObject.setCellTextStyle(row_id, cell_ind, style);
    }

    public void setCheckedRows(int col_ind, boolean checked) {
        gridObject.setCheckedRows(col_ind, checked);
    }

    public void setColAlign(String[] alignArray) {
        gridObject.setColAlign(alignArray);
    }

    public void setColSorting(String[] sortingArray) {
        gridObject.setColSorting(sortingArray);
    }

    public void setColTypes(String[] typesArray) {
        gridObject.setColTypes(typesArray);
    }

    public void setColVAlign(String[] vAligns) {
        gridObject.setColVAlign(vAligns);
    }

    public void setColWidth(int col_ind, String width) {
        gridObject.setColWidth(col_ind, width);
    }

    public void setColumnColor(String[] colors) {
        gridObject.setColumnColor(colors);
    }

    public void setColumnId(int col_ind, String id) {
        gridObject.setColumnId(col_ind, id);
    }

    public void setColumnIds(String ids) {
        gridObject.setColumnIds(ids);
    }

    public void setColumnLabel(int col_ind, String label, int h_row_ind) {
        gridObject.setColumnLabel(col_ind, label, h_row_ind);
    }

    public void setColumnMinWidth(int width, int col_ind) {
        gridObject.setColumnMinWidth(width, col_ind);
    }

    public void setColumnMinWidths(String[] minWidths) {
        gridObject.setColumnMinWidths(minWidths);
    }

    public void setDelimiter(char delimiter) {
        gridObject.setDelimiter(delimiter);
    }

    public void setDragBehavior(String mode) {
        gridObject.setDragBehavior(mode);
    }

    public void setDragText(String single, String plural) {
        gridObject.setDragText(single, plural);
    }

    public void setEditable(boolean mode) {
        gridObject.setEditable(mode);
    }

    public void setExternalTabOrder(String start_id, String end_id) {
        gridObject.setExternalTabOrder(start_id, end_id);
    }

    public void setFooterLabel(int col_ind, String label, int row_ind) {
        gridObject.setFooterLabel(col_ind, label, row_ind);
    }

    public void setHeader(String[] headerArray) {
        gridObject.setHeader(headerArray);
    }

    public void setIconPath(String path) {
        gridObject.setIconPath(path);
    }

    public void setImagePath(String imagePath) {
        gridObject.setImagePath(imagePath);
    }

    public void setInitWidths(String[] widthsArray) {
        gridObject.setInitWidths(widthsArray);
    }

    public void setInitWidthsP(String[] widthsPrecent) {
        gridObject.setInitWidthsP(widthsPrecent);
    }

    public void setNoHeader(boolean mode) {
        gridObject.setNoHeader(mode);
    }

    public void setPagingSkin(String name) {
        gridObject.setPagingSkin(name);
    }

    public void setRowAttribute(String row_id, String attributeName, String value) {
        gridObject.setRowAttribute(row_id, attributeName, value);
    }

    public void setRowColor(String row_id, String color) {
        gridObject.setRowColor(row_id, color);
    }

    public void setRowHidden(String row_id, boolean state) {
        gridObject.setRowHidden(row_id, state);
    }

    public void setRowId(int row_ind, String row_id) {
        gridObject.setRowId(row_ind, row_id);
    }

    public void setRowTextBold(String row_id) {
        gridObject.setRowTextBold(row_id);
    }

    public void setRowTextNormal(String row_id) {
        gridObject.setRowTextNormal(row_id);
    }

    public void setRowTextStyle(String row_id, String rowStyle) {
        gridObject.setRowTextStyle(row_id, rowStyle);
    }

    public void setSkin(String skin) {
        gridObject.setSkin(skin);
    }

    public void setSortImgState(boolean state, int col_ind, String order, int row) {
        gridObject.setSortImgState(state, col_ind, order, row);
    }

    public void setStyle(String headerStyle, String cellsStyle, String selectedCellStyle, String selectedRowStyle) {
        gridObject.setStyle(headerStyle, cellsStyle, selectedCellStyle, selectedRowStyle);
    }

    public void setTabOrder(String tabOrder) {
        gridObject.setTabOrder(tabOrder);
    }

    public void showRow(String row_id) {
        gridObject.showRow(row_id);
    }

    public void sortRows(int col_ind, String sortType, String sortOrder) {
        gridObject.sortRows(col_ind, sortType, sortOrder);
    }

    public void startFastOperations() {
        gridObject.startFastOperations();
    }

    public void stopFastOperations() {
        gridObject.stopFastOperations();
    }

    public void submitAddedRows(boolean mode) {
        gridObject.submitAddedRows(mode);
    }

    public void submitOnlyChanged(boolean mode) {
        gridObject.submitOnlyChanged(mode);
    }

    public void submitOnlyRowID(boolean mode) {
        gridObject.submitOnlyRowID(mode);
    }

    public void submitOnlySelected(boolean mode) {
        gridObject.submitOnlySelected(mode);
    }

    public void submitSerialization(boolean mode) {
        gridObject.submitSerialization(mode);
    }

    public String uid() {
        return gridObject.uid();
    }

    public void uncheckAll() {
        gridObject.uncheckAll();
    }

    public void unmarkAll() {
        gridObject.unmarkAll();
    }

    public void updateFromXML(String url, boolean insert_new_item, boolean del_missed, boolean forceEvent) {
        gridObject.updateFromXML(url, insert_new_item, del_missed, forceEvent);
    }

    public void setHeader(Column[] columns) {
        String[] header = new String[columns.length];
        String[] widths = new String[columns.length];
        String[] aligns = new String[columns.length];
        String[] types = new String[columns.length];
        String[] sorting = new String[columns.length];
        String[] color = new String[columns.length];

        for (int i = 0; i < columns.length; i++) {
            header[i] = columns[i].getLabel();
            widths[i] = columns[i].getWidth();
            aligns[i] = columns[i].getAlign();
            types[i] = columns[i].getType();
            sorting[i] = columns[i].getSorting();
            color[i] = columns[i].getSorting();
            if (columns[i].getMinWidth() > 0) {
               gridObject.setColumnMinWidth(columns[i].getMinWidth(), i);
            }
        }
        gridObject.setHeader(header);
        gridObject.setInitWidths(widths);
        gridObject.setColAlign(aligns);
        gridObject.setColTypes(types);
        gridObject.setColSorting(sorting);
        gridObject.setColumnColor(color);
    }

    public Object getRowDataValue(int rowInd) {
        if (values.containsKey(rowInd)) {
            return values.get(rowInd);
        } else {
            return null;
        }
    }

    public Object getRowDataValue(String rowId) {
        if (values.containsKey(getRowIndex(rowId))) {
            return values.get(getRowIndex(rowId));
        } else {
            return null;
        }
    }
}
